package com.shgsh.platform.controller;

import com.shgsh.attribute.model.ActionData;
import com.shgsh.platform.service.ActionDataService;
import com.shgsh.platform.util.SectionName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by jessy on 2015/8/8.
 */
@Controller
@RequestMapping("/platform/action")
public class ActionDataController {
    Logger log = LoggerFactory.getLogger(ActionDataController.class);

    @Autowired
    ActionDataService actionDataService;


    @RequestMapping(value = "edit",method = RequestMethod.GET)
    public String edit(String action_id,Model model) throws UnsupportedEncodingException {
        if(!StringUtils.isEmpty(action_id)){
            ActionData actionData = actionDataService.get(action_id);
            String sectionName = SectionName.returnName(actionData.getSection());
            model.addAttribute("section",actionData.getSection());
//            model.addAttribute("sectionName",new String(sectionName.getBytes("UTF-8"),"GBK"));
            model.addAttribute("actionData", actionData);
        }
        List<String> datas = actionDataService.listAttributes();
        model.addAttribute("datas",datas);
        log.debug("request action form .");
        return "/platform/action/edit";
    }

    @RequestMapping(value = "edit",method = RequestMethod.POST)
    public String edit(ActionData actionData){
        if(actionData != null){
            if(StringUtils.isEmpty(actionData.getAction_id())){
                actionDataService.save(actionData);
            }else{
                actionDataService.update(actionData);
            }
        }
        return "redirect:/platform/action/list";
    }

    @RequestMapping(value = "list",method = RequestMethod.GET)
    public String list(Model model){
        List<ActionData> actionDataList = actionDataService.list();
        /*for(ActionData ad :actionDataList){
            String[] splitResult = StringUtils.split(ad.getEffect(),"null");
            System.out.println("s -- " + splitResult);
            StringBuilder sb = new StringBuilder();
            for(String s:splitResult){
                if(!s.equals("")){
                    sb.append(s+",");
                }
            }
            System.out.println("sb ---- " + sb);
            ad.setEffect(sb.toString());
            actionDataList.add(ad);
        }*/
        model.addAttribute("actionDataList",actionDataList);
        return "/platform/action/list";
    }

    @RequestMapping(value = "delete",method = RequestMethod.GET)
    public String delete(@RequestParam(value = "action_id",required = true
    )String action_id){
        actionDataService.delete(action_id);
        return "redirect:/platform/action/list";
    }

}
